package com.tuozixuan.onesql.ui;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.tuozixuan.onesql.mysql.BaseDao;
import com.tuozixuan.onesql.service.TableContext;
import com.tuozixuan.onesql.service.TableService;
import com.tuozixuan.onesql.service.TableServiceImpl;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;

public class PageBox {
    
    private HBox box;
    
    private long pageSize = 2;
    
    public PageBox(TableContext tableContext, TableView table) {
        box = new HBox();
        Hyperlink firstPageLink = new Hyperlink("第一页");
        Hyperlink previousPageLink = new Hyperlink("上一页");
        TextField goPageTextField = new TextField();
        goPageTextField.setMaxWidth(50);
        goPageTextField.setText("1");
        Hyperlink nextPageLink = new Hyperlink("下一页");
        Hyperlink lastPageLink = new Hyperlink("最后一页");
        box.getChildren().addAll(firstPageLink, previousPageLink, goPageTextField, nextPageLink, lastPageLink);
        
        firstPageLink.setOnAction(event -> {
            int pageNo = 1;
            goPageTextField.setText(String.valueOf(pageNo));
            // 翻页处理TODO
            long startNo = (pageNo -1) * pageSize;
            ObservableList<Map<String,Object>> tableData = this.buildTableData(tableContext.getTableName(), null, startNo, pageSize);
            table.setItems(tableData);
            
        });
        previousPageLink.setOnAction(event -> {
            
            String pageNoStr = goPageTextField.getText(); // 需考虑数据是否合法 TODO
            
            int pageNo = Integer.parseInt(pageNoStr) - 1;
            if (pageNo < 1) {
                pageNo = 1;
            }
            
            goPageTextField.setText(String.valueOf(pageNo));
            // 翻页处理TODO
            long startNo = (pageNo -1) * pageSize;
            ObservableList<Map<String,Object>> tableData = this.buildTableData(tableContext.getTableName(), null, startNo, pageSize);
            table.setItems(tableData);
            
        });
        nextPageLink.setOnAction(event -> {
            
            String pageNoStr = goPageTextField.getText(); // 需考虑数据是否合法 TODO
            
            int pageNo = Integer.parseInt(pageNoStr) + 1;
            goPageTextField.setText(String.valueOf(pageNo));
            // 翻页处理TODO
            long startNo = (pageNo -1) * pageSize;
            ObservableList<Map<String,Object>> tableData = this.buildTableData(tableContext.getTableName(), null, startNo, pageSize);
            table.setItems(tableData);
            
        });
        lastPageLink.setOnAction(event -> {
            
            TableService tableService = new TableServiceImpl();
            long totalSize = tableService.getTableDataCount(tableContext.getTableName(), null);
            long pageNo = 1;
            if (totalSize % pageSize == 0) {
                pageNo = totalSize / pageSize;
            } else {
                pageNo = totalSize / pageSize + 1;
            }
            
            goPageTextField.setText(String.valueOf(pageNo));
            // 翻页处理TODO
            long startNo = (pageNo -1) * pageSize;
            ObservableList<Map<String,Object>> tableData = this.buildTableData(tableContext.getTableName(), null, startNo, pageSize);
            table.setItems(tableData);
        });
        goPageTextField.setOnKeyPressed(keyEvent -> {
            if (keyEvent.getCode() == KeyCode.ENTER) {
                System.out.println("-----enter-----");
                // 翻页处理TODO
                String pageNoStr = goPageTextField.getText();
                int pageNo = Integer.parseInt(pageNoStr);
                long startNo = (pageNo -1) * pageSize;
                ObservableList<Map<String,Object>> tableData = this.buildTableData(tableContext.getTableName(), null, startNo, pageSize);
                table.setItems(tableData);
            } 
        });
        
        goPageTextField.setTextFormatter(new TextFormatter<String>(change -> {
            System.out.println("changeText:" + change.getText());
            if (!change.getText().matches("[0-9\b]*?")) {
                return null;
            }
            
            return change;
        }));
        
    }
    
    private ObservableList<Map<String,Object>> buildTableData(String tableName, String whereConditon, Long startNo, Long pageSize) {
        TableService tableService = new TableServiceImpl();
        List<Map<String,Object>> dataList = tableService.getTableData(tableName, null, startNo, pageSize);
        ObservableList<Map<String,Object>> tableData = FXCollections.observableArrayList(dataList);
        return tableData;
    }

    public HBox getBox() {
        return box;
    }

    public void setBox(HBox box) {
        this.box = box;
    }
    
}
